package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.relatives.RelativesGift;
import com.yanqu.road.entity.relatives.RelativesInfo;
import com.yanqu.road.entity.relatives.RelativesLetter;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.relatives.RelativesProto.*;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

public class RelativesPb {
    public static RelativesTempMsg.Builder parseRelativesTempMsg(long userId, UserBaseInfo relatives, RelativesInfo relativesInfo, int redNum) {
        if(null == relatives || null == relativesInfo){
            return null;
        }
        if (relativesInfo.getMarriageTimes() == 0) {
            return null;
        }
        RelativesTempMsg.Builder msg = parseRelativesTemMsp(userId, relativesInfo, redNum);
        msg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(relatives));
        return msg;
    }
    public static RelativesTempMsg.Builder parseRelativesTempMsg(long userId, PlayerProto.PlayerBaseTempMsg relatives, RelativesInfo relativesInfo, int redNum) {
        if(null == relatives || null == relativesInfo){
            return null;
        }
        if (relativesInfo.getMarriageTimes() == 0) {
            return null;
        }
        RelativesTempMsg.Builder msg = parseRelativesTemMsp(userId, relativesInfo, redNum);
        msg.setPlayerInfo(relatives);
        return msg;
    }

    private static RelativesTempMsg.Builder parseRelativesTemMsp(long userId, RelativesInfo relativesInfo, int redNum) {
        RelativesTempMsg.Builder msg = RelativesTempMsg.newBuilder();
        msg.setRelativesId(userId);
        msg.setMarriageTimes(relativesInfo.getMarriageTimes());
        msg.setStatus(relativesInfo.getStatus());
        msg.setRedNum(redNum);
        String[] split = relativesInfo.getIsShow().split(",");
        boolean isShow;
        if (relativesInfo.getUserId1() == userId) {
            isShow = "0".equals(split[1]) ? false : true;
        } else {
            isShow = "0".equals(split[0]) ? false : true;
        }
        msg.setIsShow(isShow);
        return msg;
    }

    public static RelativesTempMsg.Builder parseRelativesTempMsg(long relativesId, UserBaseInfo relatives, RelativesInfo relativesInfo) {
        if(null == relativesInfo){
            return null;
        }
        if (relativesInfo.getMarriageTimes() == 0) {
            return null;
        }
        RelativesTempMsg.Builder msg = RelativesTempMsg.newBuilder();
        msg.setRelativesId(relativesId);
        msg.setMarriageTimes(relativesInfo.getMarriageTimes());
        msg.setStatus(relativesInfo.getStatus());
        msg.setPlayerInfo(PlayerBasePb.parsePlayerBaseTempMsg(relatives));
        return msg;
    }

    public static LetterRecordTempMsg.Builder parseLetterRecordTempMsg(long relativesId, RelativesLetter letter) {
        if(null == letter){
            return null;
        }
        LetterRecordTempMsg.Builder msg = LetterRecordTempMsg.newBuilder();
        msg.setLetterId(letter.getId());
        msg.setContent(letter.getContent());
        if (letter.getFromId() == relativesId) {
            msg.setGiveOrReceive(1);
        } else {
            msg.setGiveOrReceive(0);
        }
        msg.setIsShow(letter.isShow());
        msg.setCreateTime(letter.getCreateTime());
        return msg;
    }
    public static LetterListRespMsg.Builder parseLetterListMsg(long relativesId, int writeLetterNum, int receiveLetterNum, List<RelativesLetter> letterList) {
        LetterListRespMsg.Builder msg = LetterListRespMsg.newBuilder();
        msg.setWriteLetterNum(writeLetterNum);
        msg.setReceiveLetterNum(receiveLetterNum);
        if(null != letterList){
            for (RelativesLetter letter : letterList) {
                msg.addLetterList(parseLetterRecordTempMsg(relativesId, letter));
            }
        }
        return msg;
    }

    public static GiftListMsg.Builder parseGiftTempMapMsg(Map<Long, RelativesGift> giveMap, Map<Long, RelativesGift> receiveMap) {
        Set<Long> relativesIds = new HashSet<>();
        GiftListMsg.Builder msg = GiftListMsg.newBuilder();
        AtomicInteger giveTimes = new AtomicInteger(0);
        AtomicInteger receiveTimes = new AtomicInteger(0);
        giveMap.forEach((k,v)->{
            relativesIds.add(k);
            GiftTempMsg.Builder data = GiftTempMsg.newBuilder();
            data.setRelativesId(k);
            data.setGive(1);
            giveTimes.getAndIncrement();
            if (!receiveMap.containsKey(k)) {
                data.setReceive(0);
            } else if (receiveMap.get(k).isGet()) {
                data.setReceive(2);
                receiveTimes.getAndIncrement();
            } else {
                data.setReceive(1);
            }
            msg.addGiftList(data);
        });
        receiveMap.forEach((k,v)->{
            if (!relativesIds.contains(k)) {
                GiftTempMsg.Builder data = GiftTempMsg.newBuilder();
                data.setRelativesId(k);
                data.setGive(0);
                if (v.isGet()) {
                    data.setReceive(2);
                    receiveTimes.getAndIncrement();
                } else {
                    data.setReceive(1);
                }
                msg.addGiftList(data);
            }
        });
        msg.setGiveTimes(giveTimes.intValue());
        msg.setReceiveTimes(receiveTimes.intValue());
        return msg;
    }
}
